import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH = 800
HEIGHT = 600
WHITE = (255, 255, 255)
GREEN = (0, 128, 0)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("足球游戏")

# 球员类
class Player:
    def __init__(self, x, y, color):
        self.x = x
        self.y = y
        self.color = color
        self.speed = 5

    def move_up(self):
        if self.y > 0:
            self.y -= self.speed

    def move_down(self):
        if self.y < HEIGHT - 30:
            self.y += self.speed

    def move_left(self):
        if self.x > 0:
            self.x -= self.speed

    def move_right(self):
        if self.x < WIDTH - 30:
            self.x += self.speed

    def draw(self):
        pygame.draw.rect(screen, self.color, (self.x, self.y, 30, 30))

# 足球类
class Football:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        self.radius = 15
        self.vx = 0
        self.vy = 0
        self.friction = 0.98

    def kick(self, power_x, power_y):
        self.vx = power_x
        self.vy = power_y

    def move(self):
        self.x += self.vx
        self.y += self.vy
        self.vx *= self.friction
        self.vy *= self.friction
        # 边界检测
        if self.x - self.radius < 0 or self.x + self.radius > WIDTH:
            self.vx = -self.vx
        if self.y - self.radius < 0 or self.y + self.radius > HEIGHT:
            self.vy = -self.vy

    def draw(self):
        pygame.draw.circle(screen, YELLOW, (int(self.x), int(self.y)), self.radius)

# 球门类
class Goal:
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def draw(self):
        pygame.draw.rect(screen, RED, (self.x, self.y, self.width, self.height))

    def is_scoring(self, ball):
        ball_bottom = ball.y + ball.radius
        ball_top = ball.y - ball.radius
        ball_left = ball.x - ball.radius
        ball_right = ball.x + ball.radius
        return (ball_bottom >= self.y and
                ball_top <= self.y + self.height and
                (ball_left >= self.x and ball_right <= self.x + self.width))


# 创建游戏元素
player1 = Player(100, 300, WHITE)
player2 = Player(700, 300, BLACK)
ball = Football(400, 300)
goal1 = Goal(0, 250, 20, 100)
goal2 = Goal(780, 250, 20, 100)

# 得分
score1 = 0
score2 = 0

# 游戏主循环
clock = pygame.time.Clock()
running = True
while running:
    screen.fill(GREEN)

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                player1.move_up()
            elif event.key == pygame.K_DOWN:
                player1.move_down()
            elif event.key == pygame.K_LEFT:
                player1.move_left()
            elif event.key == pygame.K_RIGHT:
                player1.move_right()
            elif event.key == pygame.K_SPACE:
                # 踢球逻辑
                dx = ball.x - player1.x
                dy = ball.y - player1.y
                distance = (dx ** 2 + dy ** 2) ** 0.5
                if distance < 50:
                    power = 10
                    ball.kick(power * dx / distance, power * dy / distance)

    # 电脑球员的简单 AI
    if ball.x > 400:
        if ball.y > player2.y:
            player2.move_down()
        elif ball.y < player2.y:
            player2.move_up()
        if abs(ball.x - player2.x) < 50:
            dx = ball.x - player2.x
            dy = ball.y - player2.y
            distance = (dx ** 2 + dy ** 2) ** 0.5
            power = 10
            ball.kick(power * dx / distance, power * dy / distance)

    # 足球移动
    ball.move()

    # 检查得分
    if goal2.is_scoring(ball):
        score1 += 1
        ball.x = 400
        ball.y = 300
        ball.vx = 0
        ball.vy = 0
    elif goal1.is_scoring(ball):
        score2 += 1
        ball.x = 400
        ball.y = 300
        ball.vx = 0
        ball.vy = 0

    # 绘制游戏元素
    player1.draw()
    player2.draw()
    ball.draw()
    goal1.draw()
    goal2.draw()

    # 显示得分
    font = pygame.font.Font(None, 36)
    score_text = font.render(f"玩家1: {score1}  玩家2: {score2}", True, WHITE)
    screen.blit(score_text, (300, 10))

    # 更新显示
    pygame.display.flip()
    clock.tick(60)

# 退出游戏
pygame.quit()